package com.myh.exam.controller;

import com.myh.exam.dto.User;
import com.myh.exam.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {

    private UserService userService = new UserService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        System.out.println(name);
        String passwd = req.getParameter("passwd");
        System.out.println(passwd);
        User user = userService.selectUser(name, passwd);
        if (user == null) {
            resp.sendRedirect("index.jsp");
        } else {
            //1.获取列表数据
            List<User> userList = userService.selectUserList(new User(), 0, 100);
            //2.将数据传给下一个页面
            req.setAttribute("list", userList);
            req.getRequestDispatcher("listUser.jsp").forward(req, resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
